package com.drdg.netty.service;

import io.netty.channel.ChannelHandlerContext;

import com.drdg.netty.agreement.MsgAgreement;
import com.drdg.netty.bean.InformationPacket;
import com.drdg.netty.bean.InformationPacket.Group;

public class LoginMessageHanlder implements IMessageHandler {


	public Boolean doWork(MsgAgreement msgAgree, ChannelHandlerContext ctx,
			Group group) {		
		try {
			
			//登录
			InformationPacket.Login login = group.getLogin();			
			//返回登录信息
			ctx.writeAndFlush(msgAgree.doGetLoginInfoPacket(login.getUserName(), login.getUserPwd(), InformationPacket.Login.LoinEnum.Success, "OK"));
			//组织好友列表信息
			InformationPacket.Group.User.Builder userBean = InformationPacket.Group.User.newBuilder();
			userBean.setId(ctx.channel().id().toString());
			userBean.setUserName(login.getUserName());
			userBean.setUserPwd("");
			MsgHandleService.userList.put(ctx.channel().id().toString(), userBean.build());			
			//群发送好友列表
			MsgHandleService.channelGroup.writeAndFlush(msgAgree.doGetChatFriendsListInfoPacket(MsgHandleService.userList.values()));
			return true;
			
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}

		
	}

}
